/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipo.mm.web.util.pager;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import org.ipo.mm.ejb.entities.Actions;
import org.ipo.mm.ejb.facade.actions.ActionsFacadeLocal;

/**
 *
 * @author makcro
 */
public class ActionsPager extends AbstractPager {

    public ActionsPager() {
        resultsTotal = lookupActionsFacade().getCount();
    }




    @Override
    protected void injectData(HttpServletRequest request) {

        List<Actions> actions = lookupActionsFacade().findAllNotDeleted(firstResult, maxResultsPerPage);

//        String actionsFormId = "ActionsListBean";
//
//        ActionsListBean actionsBean = (ActionsListBean) request.getSession().getAttribute(actionsFormId);
//        if(actionsBean != null){
//            actionsBean.setActions(actions.toArray(new Actions[actions.size()]));
//        }
//        else {
//            ActionsListBean ab = new ActionsListBean();
//            ab.setActions(actions.toArray(new Actions[actions.size()]));
//            request.getSession().setAttribute(actionsFormId, ab);
//        }

        request.getSession().setAttribute("ACTIONS_SEARCH_RESULTS", actions.toArray(new Actions[actions.size()]));
        request.setAttribute("ACTIONS_SEARCH_PAGENUMBERS", generatePageNumbers());
        request.setAttribute("ACTIONS_SEARCH_TOTAL_RESULTS", this.resultsTotal);
        request.setAttribute("ACTIONS_SEARCH_SHOW_NEXT", new Boolean(this.showNext));
        request.setAttribute("ACTIONS_SEARCH_SHOW_PREV", new Boolean(this.showPrev));

    }

    @Override
    protected void setPageSize() {
        maxResultsPerPage = 50;
    }

    private ActionsFacadeLocal lookupActionsFacade() {
        try {
            Context c = new InitialContext();
            return (ActionsFacadeLocal) c.lookup("java:comp/env/ActionsFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
